#!/bin/bash

set -e

dpkg --assert-working-epoch 2>/dev/null || {
    echo -e "\nYou must upgrade dpkg before installing this package.\n"
    false
}

echo ""
echo "If you do not have the apt-packages installed you can install them with:"
echo ""
echo "sudo apt-get install \\"
echo "mailx postfix postfix-mysql postfix-pcre \\"
echo "python-policyd-spf postfix-policyd-spf-python python-spf \\"
echo "libsasl2-2 libsasl2-modules libsasl2-modules-sql \\"
echo "dspam libdspam7 libdspam7-drv-mysql \\"
echo "clamav clamav-base clamav-daemon clamav-freshclam libclamav3 \\"
echo "mysql-server-5.0 mysql-client-5.0 \\"
echo "courier-authdaemon courier-base courier-maildrop \\"
echo "courier-pop courier-ssl courier-pop-ssl \\"
echo "courier-authlib courier-authlib-userdb courier-authlib-mysql "
echo ""
echo ""

# TODO: Create some sort of check for required packages
echo -en "Have you installed all of the required packages? [N/y]: "
read temp; INPUT_PKGS_INSTALLED=${temp:-N}

if [ $INPUT_PKGS_INSTALLED = "Y" ]
then
	INPUT_PKGS_INSTALLED="y"
fi

if [ $INPUT_PKGS_INSTALLED != "y" ]
then
	echo ""
	exit
fi

echo ""

if [ -d /etc/postfix ]
then
  if [ ! -d /etc/postfix.bak ]
  then
    cp -r /etc/postfix /etc/postfix.bak
  fi
fi

if [ -d /etc/dspam ]
then
  if [ ! -d /etc/dspam.bak ]
  then
    cp -r /etc/dspam /etc/dspam.bak
  fi
fi

if [ -d /etc/clamav ]
then
  if [ ! -d /etc/clamav.bak ]
  then
    cp -r /etc/clamav /etc/clamav.bak
  fi
fi

echo "Stopping services..."
/etc/init.d/postfix stop >> /dev/null
/etc/init.d/dspam stop >> /dev/null
/etc/init.d/clamav-daemon stop >> /dev/null
/etc/init.d/courier-authdaemon stop

if [ -f /etc/init.d/courier-pop ]
then
	/etc/init.d/courier-pop stop
fi

if [ -f /etc/init.d/courier-pop-ssl ]
then
	/etc/init.d/courier-pop-ssl stop
fi

if [ -f /etc/init.d/courier-imap ]
then
	/etc/init.d/courier-imap stop
fi

if [ -f /etc/init.d/courier-imap-ssl ]
then
	/etc/init.d/courier-imap-ssl stop
fi

# EOF
